package top.jnaw.jee.platform;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Const;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;
import com.jfinal.template.Engine;
import top.jnaw.jee.platform.model._MappingKit;
import top.jnaw.jee.platform.routers.BasicExtendRouters;
import top.jnaw.jee.platform.routers.BasicOrgRouters;
import top.jnaw.jee.platform.routers.SDRouters;
import top.jnaw.jee.plugin.shiro.ShiroInterceptor;
import top.jnaw.jee.utils.Cache;
import top.jnaw.jee.utils.Generator;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.RedisCache;
import top.jnaw.jee.utils.quartz.QuartzManager;

/**
 * Created by lc on 17-10-12.
 */
public class Config extends JFinalConfig {

  private IPlugin shiroPlugin;
  private IDataSourceProvider provider;

  @Override
  public void configConstant(Constants me) {
    // [Neo] init config file and log
    PropKit.use("developer.properties");
//    PropKit.use("product.properties");
    Log.init(Consts.PROJECT_NAME);

    me.setDevMode(PropKit.getBoolean("dev_mode"));
    me.setMaxPostSize(10 * Const.DEFAULT_MAX_POST_SIZE);
    provider = Generator.dataSourcePlugin();
  }

  @Override
  public void configRoute(Routes me) {
    me.add(new BasicOrgRouters());
    me.add(new BasicExtendRouters());
    // [Neo] TODO add more routes
    me.add(new SDRouters());

    shiroPlugin = Generator.shiroPlugin(provider.getDataSource(), me);
    shiroPlugin.start();
  }

  @Override
  public void configEngine(Engine me) {
  }

  @Override
  public void configPlugin(Plugins me) {
    ActiveRecordPlugin arp = new ActiveRecordPlugin(provider);
    arp.setShowSql(PropKit.getBoolean("show_sql"));
//    arp.setCache(new RedisCache());

    Models.addSqlTemplate(arp);

    // TODO [Neo] after test case has run, uncomment this line
    _MappingKit.mapping(arp);

    me.add(arp);
//    me.add(Generator.cachePlugin());

//    QuartzPlugin quartzPlugin = new QuartzPlugin();
//    quartzPlugin.setJobs("job.properties");
//    me.add(quartzPlugin);
  }

  @Override
  public void configInterceptor(Interceptors me) {
    me.add(new ShiroInterceptor());
  }

  @Override
  public void configHandler(Handlers me) {
    me.add(Generator.statHandler(PropKit.get("druid_stat_view")));

    // [Neo] check redis
    if (Cache.init()) {
      Log.i("redis is online");
    } else {
      Log.w("redis is offline");
    }
  }

  @Override
  public void afterJFinalStart() {
    super.afterJFinalStart();
    //加载定时任务数据库配置
//    QuartzManager qm = new QuartzManager();
//    qm.initJob();
  }

  @Override
  public void beforeJFinalStop() {
    super.beforeJFinalStop();
    //关闭定时任务管理器
//    QuartzManager qm = new QuartzManager();
//    qm.shutdown();
  }

}
